﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class frm_LeftPage : System.Web.UI.Page
{
    private ClsMenuDAL cls_Menu = new ClsMenuDAL();

    private void ChangTabMenu(int Type)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            DataSet dst = cls_Menu.GetSysMenuTypes(strUserID, Type, -1);

            if (dst != null && dst.Tables[0].Rows.Count == 1)
            {
                this.trvTab1Menus.Nodes.Clear();
                this.trvTab2Menus.Nodes.Clear();

                string strTypeName = dst.Tables[0].Rows[0][2].ToString();
                int intTypeID = Int32.Parse(dst.Tables[0].Rows[0][0].ToString());

                TreeNode node = new TreeNode(strTypeName);

                if (Type == 0)
                {
                    this.btnTab1.ImageUrl = "App_Images/b_4_1.gif";
                    this.btnTab2.ImageUrl = "App_Images/b_5_2.gif";
                    this.btnTab3.ImageUrl = "App_Images/b_6_2.gif";
                    this.panTab1.Visible = true;
                    this.panTab2.Visible = this.panTab3.Visible = false;

                    this.trvTab1Menus.Nodes.Add(node);
                    this.trvTab1Menus.Nodes[0].Expanded = true;
                }
                else if (Type == 1)
                {
                    this.btnTab1.ImageUrl = "App_Images/b_4_2.gif";
                    this.btnTab3.ImageUrl = "App_Images/b_6_2.gif";
                    this.btnTab2.ImageUrl = "App_Images/b_5_1.gif";
                    this.panTab1.Visible = this.panTab3.Visible = false;
                    this.panTab2.Visible = true;

                    this.trvTab2Menus.Nodes.Add(node);
                    this.trvTab2Menus.Nodes[0].Expanded = true;
                }
                else
                {
                    this.btnTab1.ImageUrl = "App_Images/b_4_2.gif";
                    this.btnTab2.ImageUrl = "App_Images/b_5_2.gif";
                    this.btnTab3.ImageUrl = "App_Images/b_6_1.gif";
                    this.panTab1.Visible = this.panTab2.Visible = false;
                    this.panTab3.Visible = true;
                }

                //获取子菜单
                this.GetChildMenus(strUserID, node, intTypeID);
            }
            else
            {
                if (Type == 0)
                {
                    this.panBtn1.Visible = this.panTab1.Visible = false;

                    ChangTabMenu(1);
                }
                else if (Type == 1)
                {
                    this.panBtn2.Visible = this.panTab2.Visible = false;
                }
                else
                {
                    this.panBtn3.Visible = this.panTab3.Visible = false;
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='index.aspx'</script>";
        }
    }

    private void GetChildMenus(string UserID, TreeNode Node, int UpTypeID)
    {
        DataSet dstMenuType = cls_Menu.GetSysMenuTypes(UserID, -1, UpTypeID);

        if (dstMenuType != null && dstMenuType.Tables[0].Rows.Count > 0)
        {
            int intNodes = dstMenuType.Tables[0].Rows.Count;

            for (int i = 0; i < intNodes; i++)
            {
                string strName = dstMenuType.Tables[0].Rows[i][2].ToString();
                int intUpNenuID = Int32.Parse(dstMenuType.Tables[0].Rows[i][0].ToString());

                TreeNode childNode = new TreeNode("<a href='#' class='text' onclick=\"TreeViewExpanded("+ i.ToString() +", '"+ this.btnExpanded.ClientID +"')\">" + strName + "</a>");
                
                childNode.Expanded = false;
                Node.ChildNodes.Add(childNode);

                GetChildMenus(UserID, childNode, intUpNenuID);
            }
        }
        else
        {
            DataSet dstMenuItem = cls_Menu.GetSysMenuItems(UserID, UpTypeID);

            if (dstMenuItem != null && dstMenuItem.Tables[0].Rows.Count > 0)
            {
                int intNodes = dstMenuItem.Tables[0].Rows.Count;

                for (int i = 0; i < intNodes; i++)
                {
                    int intMenuID = Int32.Parse(dstMenuItem.Tables[0].Rows[i][0].ToString());
                    bool is_stop = bool.Parse(dstMenuItem.Tables[0].Rows[i][6].ToString());
                    string strName = dstMenuItem.Tables[0].Rows[i][2].ToString();
                    string strUrl = dstMenuItem.Tables[0].Rows[i][4].ToString();
                    string strAction = "";

                    if (!is_stop)
                    {
                        if (strUrl.Trim() != "")
                        {
                            if (strUrl.Trim() == "-1")
                            {
                                strAction = "<a href='#' onclick=\"ModalDialog('Mod_FlatManger/frm_UpdateUserPwd.aspx?set=-1', 350, 235);\" class='text'>" + strName + "</a>";
                            }
                            else if (strUrl.Trim() == "-3")
                            {
                                strAction = "<a href='#' onclick=\"ModalDialog('Mod_FlatManger/frm_ShowMe.aspx?set=-3', 465, 360);\" class='text'>" + strName + "</a>";
                            }
                            else if (strUrl.Trim() == "-2")
                            {
                                strAction = "<a href='#' onclick=\"if(confirm('您真的要退出系统吗？')) parent.window.close();\" class='text'>" + strName + "</a>";
                            }
                            else
                            {
                                strAction = "<a href='#' onclick=\"AddWin(" + i + ",'" + strName + "','" + strUrl + "', " + intMenuID + ");\" class='text'>" + strName + "</a>";
                            }
                        }
                        else
                        {
                            strAction = "<a href='#' onclick=\"alert('无URL连接地址，请与系统管理员联系！');\" class='text'>" + strName + "</a>";
                        }
                    }
                    else
                    {
                        strAction = "<a href='#' onclick=\"alert('该系统菜单已停用，请与系统管理员联系！');\" class='text'>" + strName + "</a>";
                    }

                    TreeNode childNode = new TreeNode(strAction);

                    Node.ChildNodes.Add(childNode);
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ChangTabMenu(0);
        }
        else
        {
            this.lblJs.Text = "";
        }
    }

    protected void btnTab1_Click(object sender, ImageClickEventArgs e)
    {
        this.ChangTabMenu(0);
    }

    protected void btnTab2_Click(object sender, ImageClickEventArgs e)
    {
        this.ChangTabMenu(1);
    }

    protected void btnTab3_Click(object sender, ImageClickEventArgs e)
    {
        this.ChangTabMenu(2);
    }

    protected void btnExpanded_Click(object sender, EventArgs e)
    {
        if (this.trvTab2Menus.Nodes != null && this.trvTab2Menus.Nodes.Count > 0 && this.trvTab2Menus.Nodes[0].ChildNodes != null)
        {
            try
            {
                int intMenuID = Int32.Parse(this.hidMenuID.Value);

                bool is_Expanded = (bool)this.trvTab2Menus.Nodes[0].ChildNodes[intMenuID].Expanded;

                this.trvTab2Menus.Nodes[0].ChildNodes[intMenuID].Expanded = (is_Expanded) ? false : true;
            }
            catch { }
        }
    }
}
